package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.pojo.DtsPermission;
import com.qf.service.DtsPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Zwnan
 */
@Service
public class DtsPermissionServiceImpl implements DtsPermissionService {

    @Autowired
    private DtsPermissionMapper dtsPermissionMapper;
    @Override
    public Set<String> findpermissionByRoleIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsPermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(DtsPermission::getPermission)
                .in(DtsPermission::getRoleId,roleIds)
                .eq(DtsPermission::getDeleted, false);
        List<DtsPermission> dtsPermissions = dtsPermissionMapper.selectList(lambdaQueryWrapper);
        return dtsPermissions.stream().map(DtsPermission::getPermission).collect(Collectors.toSet());
    }

    @Override
    public Set<String> findPermissionByRoleId(Integer roleId) {
        LambdaQueryWrapper<DtsPermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(DtsPermission::getPermission)
                .in(DtsPermission::getRoleId,roleId)
                .eq(DtsPermission::getDeleted, false);
        List<DtsPermission> dtsPermissions = dtsPermissionMapper.selectList(lambdaQueryWrapper);
        return dtsPermissions.stream().map(DtsPermission::getPermission).collect(Collectors.toSet());
    }

    @Override
    public void updatePermissions(Integer roleId, List<String> permissions) {
        DtsPermission dtsPermission = new DtsPermission();
        dtsPermission.setDeleted(true);
        dtsPermissionMapper.update(dtsPermission,new QueryWrapper<DtsPermission>().eq("role_id", roleId));
        dtsPermissionMapper.insertByRoleId(permissions,roleId,new Date(),0);
    }
}
